﻿''=========================================
'' 文件名称：MediaPlayer.vb
'' 作者：李大龙
'' 日期：2009-12-21
'' 描述：播放wav,mp3.MID

Imports System.Runtime.InteropServices

Namespace Media

    ''' <summary>
    ''' 支持WAV,MP3,WMA格式文件的播放
    ''' </summary>
    ''' <remarks></remarks>
    Public Class MediaPlayer

#Region "WIN DLL"

        ''' <summary>
        ''' 要播放的文件名称
        ''' </summary>
        ''' <remarks></remarks>
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Private Name As String = ""

        ''' <summary>
        ''' 魔法文件的长度
        ''' </summary>
        ''' <remarks></remarks>
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
        Private durLength As String = ""

        <MarshalAs(UnmanagedType.LPTStr, SizeConst:=128)> _
        Private TemStr As String = ""

        <DllImport("winmm.dll", entrypoint:="mciSendString", CharSet:=CharSet.Auto)> _
        Protected Shared Function MCISendString(ByVal lpStrCommand As String, ByVal lpStrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallBack As Integer) As Integer
        End Function

        <DllImport("kernel32.dll", Entrypoint:="GetShortPathName", CharSet:=CharSet.Auto)> _
        Protected Shared Function GetShorPathName(ByVal lpszLongPath As String, ByVal shortFile As String, ByVal cchBuffer As Integer) As Integer
        End Function
#End Region


        ''' <summary>
        ''' 播放器状态
        ''' </summary>
        ''' <remarks></remarks>
        Public Enum State
            mPlaying = 1
            mPaused = 2
            mStopped = 3
        End Enum

        Friend Structure StructMCI
            Public bMut As Boolean
            Public iDur As Integer
            Public iPos As Integer
            Public iVol As Integer
            Public iBal As Integer
            Public iName As String
            Public state As State
        End Structure

        Private MCI As StructMCI = New StructMCI()

        Private iLong As Integer = 0

        ''' <summary>
        ''' 播放器状态
        ''' </summary>
        Public Function PlayerState() As State
            Return MCI.state
        End Function

        ''' <summary>
        ''' 设置要播放的文件
        ''' </summary>
        Public Property FileName() As String
            Get
                Return MCI.iName
            End Get
            Set(ByVal value As String)
                Try
                    TemStr = ""
                    TemStr = TemStr.PadLeft(127, Convert.ToChar(" "))
                    Name = Name.PadLeft(260, Convert.ToChar(" "))

                    MCI.iName = value
                    iLong = GetShorPathName(MCI.iName, Name, Name.Length)
                    Name = GetCurrentPath(Name)
                    Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media"
                    iLong = MCISendString("close all", TemStr, TemStr.Length, 0)
                    iLong = MCISendString(Name, TemStr, TemStr.Length, 0)
                    iLong = MCISendString("set media time format milliseconds", TemStr, TemStr.Length, 0)
                    MCI.state = State.mStopped
                Catch ex As Exception
                    System.Diagnostics.Debug.WriteLine(ex.Message)
                End Try
            End Set
        End Property

        ''' <summary>
        ''' 播放当前文件
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub play()
            Try

                TemStr = ""
                TemStr = TemStr.PadLeft(128, Convert.ToChar(" "))
                MCISendString("play media", TemStr, TemStr.Length, 100)
                MCI.state = State.mPlaying

            Catch ex As Exception
                System.Diagnostics.Debug.WriteLine(ex.Message)
            End Try
        End Sub

        ''' <summary>
        ''' 停止当前的播放
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub [Stop]()
            Try
                TemStr = ""
                TemStr = TemStr.PadLeft(128, Convert.ToChar(" "))
                MCISendString("close media", TemStr, 128, 0)
                MCISendString("close all", TemStr, 128, 0)
                MCI.state = State.mStopped
            Catch ex As Exception
                System.Diagnostics.Debug.WriteLine(ex.Message)
            End Try
        End Sub


        ''' <summary>
        ''' 暂停当前的播放
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Pause()
            Try
                If (MCI.state = State.mPlaying) Then

                    TemStr = ""
                    TemStr = TemStr.PadLeft(128, Convert.ToChar(" "))
                    MCISendString("pause media", TemStr, 128, 0)
                    MCI.state = State.mPaused
                End If
            Catch ex As Exception
                System.Diagnostics.Debug.WriteLine(ex.Message)
            End Try
        End Sub

        ''' <summary>
        ''' 回复暂停，继续播放文件
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub [Resume]()
            Try
                If (MCI.state = State.mPaused) Then

                    TemStr = ""
                    TemStr = TemStr.PadLeft(128, Convert.ToChar(" "))
                    MCISendString("resume media", TemStr, 128, 0)
                    MCI.state = State.mPlaying
                End If
            Catch ex As Exception
                System.Diagnostics.Debug.WriteLine(ex.Message)
            End Try
        End Sub

        ''' <summary>
        ''' 播放文件的时间长度
        ''' </summary>
        Public Function Duration() As Integer
            Try
                durLength = ""
                durLength = durLength.PadLeft(128, Convert.ToChar(" "))
                MCISendString("status media length", durLength, durLength.Length, 0)
                durLength = durLength.Trim()
                If (durLength = "") Then
                    Return 0
                Else
                    Return CInt(Convert.ToDouble(durLength) / 1000.0F)
                End If
            Catch ex As Exception
                Return 0
            End Try
        End Function

        ''' <summary>
        ''' 播放文件的当前时间
        ''' </summary>
        ''' <remarks></remarks>
        Public Function CurrentPosition() As Integer
            Try
                durLength = ""
                durLength = durLength.PadLeft(128, Convert.ToChar(" "))
                MCISendString("status media position", durLength, durLength.Length, 0)
                durLength = durLength.Trim()
                MCI.iPos = CInt(Convert.ToDouble(durLength) / 1000.0F)
                Return MCI.iPos
            Catch
                Return 0
            End Try
        End Function

        ''' <summary>
        ''' 获取当前播放文件的路径
        ''' </summary>
        ''' <remarks></remarks>
        Private Function GetCurrentPath(ByVal Name As String) As String
            If (Name.Length < 1) Then Return ""

            Name = Name.Trim()
            Name = Name.Substring(0, Name.Length - 1)
            Return Name
        End Function
    End Class
End Namespace
